package IntentMessage;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import com.example.myapplication.R;

public class IntentSubDialActivity extends AppCompatActivity {
  @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.intent_test_sublayout2);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle("拨打电话");
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    SetContactButton();
    SetDialButton();
  }

  @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        GoBack();
    }
    return true;
  }

  private void GoBack() {
    Intent intent = new Intent(this, IntentActivity.class);
    startActivity(intent);
  }
  private void SetContactButton() {
    Button button = (Button) findViewById(R.id.button_contact);
    button.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        ContactIntent();
      }
    });
  }
  private void SetDialButton() {
    Button button = (Button) findViewById(R.id.button_dial);
    button.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        DialIntent();
      }
    });
  }
  private void ContactIntent() {
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    intent.setData(ContactsContract.Contacts.CONTENT_URI);
    startActivity(intent);

  }
  private void DialIntent() {
    String dialNumber="";
    EditText editText = (EditText) findViewById(R.id.inputBox);
    dialNumber= String.valueOf(editText.getText());
    if (dialNumber.equals("")) {
      return;
    }
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_DIAL);
    intent.setData(Uri.parse("tel:"+dialNumber));

    startActivity(intent);
  }
}
